//
//  HMLoginController.m
//  HeartMark
//
//  Created by rimi on 16/11/9.
//  Copyright © 2016年 黄华强. All rights reserved.
//

#import "HMLoginController.h"
#import <Masonry.h>
#import <AVOSCloud/AVOSCloud.h>
#import "HMRegisteredController.h"
#import "HMTabBarViewController.h"
#import "HMFindPassWordController.h"

@interface HMLoginController ()

@property(nonatomic,strong)UITextField* userT;
@property(nonatomic,strong)UITextField* passwordT;
@end

@implementation HMLoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    //设置背景图
    UIImageView* bgImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    bgImage.image = [UIImage imageNamed:@"login_bg.jpg"];
    [self.view addSubview:bgImage];
    //设置UI
    [self setupUI];
  
}

- (void)textFBeginEdit {
    CGRect tempRect = self.view.frame;
    tempRect.origin.y = -(SCREEN_HEIGHT * 0.15);
    self.view.frame = tempRect;
    
}


-(void)setupUI{
    
    //设置输入框
    _userT = [[UITextField alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * 0.18, SCREEN_HEIGHT * 0.33, SCREEN_WIDTH * 0.55, 44)];
    //圆角模式
    _userT.borderStyle = UITextBorderStyleRoundedRect;
    _userT.layer.masksToBounds = YES;
    _userT.placeholder = @"请输入账号";
    _userT.font = [UIFont systemFontOfSize:15];
    [_userT addTarget:self action:@selector(textFBeginEdit) forControlEvents:UIControlEventEditingDidBegin];
    [self.view addSubview:_userT];
    
    _passwordT = [[UITextField alloc]init];
    _passwordT.placeholder = @"请输入密码";
    //圆角模式
    _passwordT.borderStyle = UITextBorderStyleRoundedRect;
    _passwordT.secureTextEntry = YES;//密码模式
    _passwordT.font = [UIFont systemFontOfSize:15];
    [_passwordT addTarget:self action:@selector(textFBeginEdit) forControlEvents:UIControlEventEditingDidBegin];
    [self.view addSubview:_passwordT];
    
    [_passwordT mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_userT.mas_left);
        make.top.equalTo(_userT.mas_bottom).offset(SCREEN_HEIGHT * 0.02);
        make.width.equalTo(_userT.mas_width);
        make.height.equalTo(_userT.mas_height);
    }];
    //设置登录和注册按钮
    UIButton* loginBtn = [[UIButton alloc]init];
    loginBtn.backgroundColor = [UIColor orangeColor];
    loginBtn.layer.cornerRadius = 5;
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginBtn addTarget:self action:@selector(loginBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:loginBtn];
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_passwordT.mas_bottom).offset(SCREEN_HEIGHT * 0.04);
        make.left.equalTo(_passwordT.mas_left);
        int w = SCREEN_WIDTH * 0.25;
        NSNumber *nw = [NSNumber numberWithInt:w];
        make.width.equalTo(nw);
        make.height.equalTo(@40);
    }];
    
    UIButton* registeredBtn = [[UIButton alloc]init];
    registeredBtn.layer.cornerRadius = 5;
    registeredBtn.backgroundColor = [UIColor orangeColor];
    
    [registeredBtn setTitle:@"注册" forState:UIControlStateNormal];
    [registeredBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [registeredBtn addTarget:self action:@selector(registeredBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:registeredBtn];
    
    [registeredBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(loginBtn.mas_centerY);
        make.left.equalTo(loginBtn.mas_right).offset(SCREEN_WIDTH * 0.05);
        make.width.equalTo(loginBtn.mas_width);
        make.height.equalTo(loginBtn.mas_height);
    }];
    //找回密码按钮
    UIButton* findPasswordBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [findPasswordBtn setTitle:@"找回密码" forState:UIControlStateNormal];
    findPasswordBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [findPasswordBtn setTintColor:[UIColor blackColor]];
    [findPasswordBtn addTarget:self action:@selector(findPassWordBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:findPasswordBtn];
    [findPasswordBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(loginBtn.mas_left).offset(-8);
        make.top.equalTo(loginBtn.mas_bottom).offset(8);
        make.width.equalTo(loginBtn.mas_width);
        make.height.equalTo(loginBtn.mas_height);
    }];
    
}
//找回密码按钮回调事件
-(void)findPassWordBtnClicked{
    HMFindPassWordController* findVc = [[HMFindPassWordController alloc]init];
    [self presentViewController:findVc animated:YES completion:nil];
}
//登录按钮回调事件
-(void)loginBtnClicked:(UIButton *)button{
    [self.view endEditing:YES];
    CGRect tempRect = self.view.frame;
    tempRect.origin.y = 0;
    self.view.frame = tempRect;
    
    NSString *username = self.userT.text;
    NSString *password = self.passwordT.text;
    
    if ([username isEqualToString:@""]) {
        [self setAlertVc:@"用户名不能为空"];
        return;
    }
    if ([password isEqualToString:@""]) {
        [self setAlertVc:@"密码不能为空"];
        return;
    }
    
  
    [button setUserInteractionEnabled:NO];
        [AVUser logInWithUsernameInBackground:username password:password block:^(AVUser *user, NSError *error) {
            if (error) {
               
                if (error.code == -1001) {
                    [self setAlertVc:@"网络请求超时，请检查网络"];
                } else {
                    [self setAlertVc:@"用户名或者密码错误" ];
                }
                
            } else {
//                NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
//                [userDefaults setObject:@"NO" forKey:@"noteSaveArr"];
                
                HMTabBarViewController *tabBarVC = [[HMTabBarViewController alloc] init];
                UIWindow *window = [UIApplication sharedApplication].keyWindow;
                window.rootViewController = tabBarVC;
                
                [self.navigationController popViewControllerAnimated:YES];
                
            }
            [button setUserInteractionEnabled:YES];
        }];
    
    
}
//注册按钮回调事件
-(void)registeredBtnClicked{
    
    HMRegisteredController* registeredVc = [[HMRegisteredController alloc]init];
   
    [self presentViewController:registeredVc animated:YES completion:nil];
    
}



//提示框
-(void)setAlertVc:(NSString*)string{
    UIAlertController* alertVc = [UIAlertController alertControllerWithTitle:@"提示" message:string preferredStyle:UIAlertControllerStyleAlert];
    
    [alertVc addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }]];
    
    [self presentViewController:alertVc animated:YES completion:nil];
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
    CGRect tempRect = self.view.frame;
    tempRect.origin.y = 0;
    self.view.frame = tempRect;
}

@end
